//
//  NormalPrombleViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/8.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "NormalPrombleViewController.h"
#import "WebDetailInfoViewController.h"
@interface NormalPrombleViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSInteger _totle;
}

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign)   NSInteger    pageNO;
@property(nonatomic,strong)NSMutableArray * normalPrombleArray;
@end

@implementation NormalPrombleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNO =  1;
    _normalPrombleArray = [NSMutableArray array];
    if ([self.typeStr isEqualToString:@"1"]) {
        self.navgationTitleLabel.text = @"常见问题";
    }else if([self.typeStr isEqualToString:@"2"]){
        self.navgationTitleLabel.text = @"使用帮助";
    }
    [self initViewLayout];
    [self addRefreshing];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}
- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}
#pragma mark -- 添加刷新数据
- (void)addRefreshing{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        int64_t delayInSeconds = 8.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self.tableView.header endRefreshing];
        });
        self.pageNO = 1;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //请求常见问题列表
            [self getNormalPrombleDataList];
        });
    }];
    // 马上进入刷新状态
    [self.tableView.header beginRefreshing];
}
//加载更多数据
- (void)loadMoreData{
    if (self.normalPrombleArray.count == _totle) {
        [self.tableView.footer endRefreshing];
    }else{
        self.pageNO = self.pageNO + 1;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //请求常见问题列表
            [self getNormalPrombleDataList];
        });
    }
    
}
#pragma mark -- 请求常见问题列表
- (void)getNormalPrombleDataList{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic  setObject:[NSNumber numberWithInteger:self.pageNO]     forKey:@"currentPage"];
    [dic  setObject:[NSNumber numberWithInteger:PageSize]       forKey:@"pageSize"];
    if ([self.typeStr isEqualToString:@"1"]) {
        [dic setObject:@"1" forKey:@"helpId"];
    }else if([self.typeStr isEqualToString:@"2"]){
        [dic setObject:@"2" forKey:@"helpId"];
    }
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppUsingHelp withHttpType:@"POST" withTag:SX_queryAppUsingHelpTag];
    
}
#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentify = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify ];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIButton class] ]||[view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }
    UILabel * label = [[UILabel alloc]init];
    label.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
    label.text = _normalPrombleArray[indexPath.row][@"title"];
    label.frame = flexibleFrame(CGRectMake(14, 0, 200, 50), YES);
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor colorWithWhite:0.220 alpha:1.000];
    [cell addSubview:label];
    
    UIImageView  * imageView = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(300, 18, 8, 13), YES)];
    imageView.image = [UIImage imageNamed:@"jiantou"];
    [cell addSubview:imageView];

    
        UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 49, 320, 1), NO)];
        line.backgroundColor = [UIColor colorWithWhite:0.941 alpha:1.000];

        [cell addSubview:line];


    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _normalPrombleArray.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
        return 50*VerticalRatio();
  
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    WebDetailInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WebDetailInfo"];
    page.objectId = _normalPrombleArray[indexPath.row][@"usingHelp"];
    if ([self.typeStr isEqualToString:@"1"]) {
        page.titleStr = @"解决方案";
    }else if([self.typeStr isEqualToString:@"2"]){
        page.titleStr = @"使用帮助";
    }
    [self.navigationController  pushViewController:page animated:YES];
    
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    [_tableView.header endRefreshing];
    [_tableView.footer endRefreshing];
    if (tag==SX_queryAppUsingHelpTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            dispatch_async(dispatch_get_main_queue(), ^{
                _totle = [database[@"result"][@"totalResults"] integerValue];
                if (_totle > PageSize) {
                    __weak __typeof(self) weakSelf = self;
                    if (!self.tableView.footer) {
                        // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
                        self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                            [weakSelf loadMoreData];
                        }];
                    }
                }
                if (self.pageNO == 1) {
                    [self.normalPrombleArray removeAllObjects];
                }
                NSArray *arr = database[@"result"][@"pageRecord"];
                for (id entity in arr) {
                    [self.normalPrombleArray addObject:entity];
                }
                _tableView.dataSource = self;
                _tableView.delegate = self;
                [_tableView reloadData];
                
            });
            
            
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    [self dismissWaitDialog];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
